/**
 * 
 */
package mdb.com.vn.persist;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import mdb.com.vn.model.FixedDepositCasa;

/**
 * @author tu.dod
 *
 */
@Repository("fixedDepositCasaDAO")
public class FixedDepositCasaDAOImpl extends AbstractHibernateDAOImpl<FixedDepositCasa,Integer> implements FixedDepositCasaDAO,Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	@Override
	public List<FixedDepositCasa> findByPersionalType(int type) {
		// TODO Auto-generated method stub
		Query query = getSession().getNamedQuery("FixedDepositCasa.findByPersionalType").setInteger("customerType", type);
		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FixedDepositCasa> findByCorporateType(int type) {
		// TODO Auto-generated method stub
		Query query = getSession().getNamedQuery("FixedDepositCasa.findByCorporateType").setInteger("customerType", type);
		return query.list();
	}

	@Override
	protected Class<FixedDepositCasa> getDomainClass() {
		// TODO Auto-generated method stub
		return FixedDepositCasa.class;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FixedDepositCasa> findByCode(String sabookCode,
			String saAccoutCode) {
		// TODO Auto-generated method stub
		return getSession().getNamedQuery("FixedDepositCasa.findByCode").setString("savingbookNo", sabookCode).setString("accountLocked", saAccoutCode).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public int getSCardFDCSAvailable(String customerRef) {
		// TODO Auto-generated method stub
		List<FixedDepositCasa> depositCasas = getSession().getNamedQuery("FixedDepositCasa.findByCustomerRef").setString("customerId", customerRef).list();
		int totalCSAvailable = 0;
		for (FixedDepositCasa fixedDepositCasa : depositCasas) {
			totalCSAvailable += fixedDepositCasa.getScCardNotGet();
		}
		return totalCSAvailable;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FixedDepositCasa> findByCode(String sabookCode) {
		// TODO Auto-generated method stub
		return getSession().getNamedQuery("FixedDepositCasa.findByCodeSaving").setString("savingbookNo", sabookCode).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FixedDepositCasa> findByCodeCS(String saAccoutCode) {
		// TODO Auto-generated method stub
		return getSession().getNamedQuery("FixedDepositCasa.findByCodeSavingAcc").setString("accountLocked", saAccoutCode).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FixedDepositCasa> findByCustomerRef(String customerRef) {
		// TODO Auto-generated method stub
		return getSession().getNamedQuery("FixedDepositCasa.findByCustomerRef").setString("customerId", customerRef).list();
	}

	@Override
	public int getSCardFDCSAvailableException(String customerRef,
			String accountException) {
		@SuppressWarnings("unchecked")
		List<FixedDepositCasa> depositCasas = getSession().getNamedQuery("FixedDepositCasa.findByCustomerRef").setString("customerId", customerRef).list();
		int totalCSAvailable = 0;
		for (FixedDepositCasa fixedDepositCasa : depositCasas) {
			if(!fixedDepositCasa.getSavingbookNo().equals(accountException)){
				totalCSAvailable += fixedDepositCasa.getScCardNotGet();
			}
		}
		return totalCSAvailable;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FixedDepositCasa> getAllFixedDepositCasas() {
		// TODO Auto-generated method stub
		return getSession().getNamedQuery("FixedDepositCasa.getAllFixedDepositCasa").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public int countThanksCasrd() {
		// TODO Auto-generated method stub
		List<Long> longs = null;
		longs = getSession().getNamedQuery("FixedDepositCasa.countThanksCard").list();
		if (longs.size() > 0){
			if(longs.get(0)!=null){
				return longs.get(0).intValue();
			}
		}
		return 0;
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public BigDecimal sumAmount() {
		List<BigDecimal> longs = getSession().getNamedQuery("FixedDepositCasa.sumAmount").list();
		if (longs.size() > 0){
			if(longs.get(0)!=null){
				return longs.get(0) ;
			}
		}
		return new BigDecimal(0);
	}
}
